# -*- coding: utf-8 -*-

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from lxml import etree
import time
from fake_useragent import UserAgent
import re
from utils.HttpUtils import HttpUtils

"""

"""

class Chrome_Get():
    def __init__(self):
        chrome_options = Options()
        # 设置chrome不加载图片，提高速度
        # chrome_options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
        # chrome_options.add_argument('--headless') # 无头模式
        chrome_options.add_argument('--disable-gpu')
        # chrome_options.add_argument('user-agent='+UserAgent().random)  # 修改请求头，伪装浏览器型号
        chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])  # 设置开发模式，避免被检测
        chrome_options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36')
        # self.driver = webdriver.Chrome(chrome_options=chrome_options) # 已经设置PATH环境变量
        # 未在PATH环境变量中设置，需要设置驱动可执行路径
        chrome_options.add_argument("--disable-blink-features=AutomationControlled")
        self.driver = webdriver.Chrome(executable_path='D:\\webdriver\\chromedriver.exe', chrome_options=chrome_options)


        # 反爬，去掉webdriver
        with open('./stealth.min.js') as f:
            js = f.read()
        self.driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
            "source": js
        })
        self.driver.maximize_window()  # 最大化
        # html = self.driver.page_source
        # print(html)

    # 获取内容
    def get_contents(self, url):
        self.driver.get("https://dxy.pub/14xDcB")
        # # 设置等待
        # wait = WebDriverWait(self.driver, 10)

        pageNo = 1
        count = 73
        filename = "丁香医生-整外"
        while pageNo < count:
            WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.XPATH, '//div/span[@class="error"]')))

            element = self.driver.find_elements(By.XPATH, '//div/span[@class="error"]')[pageNo]
            # 因有遮挡点击不到，每次滚动条下滑
            try:
                js="var q=document.documentElement.scrollTop=100000"
                self.driver.execute_script(js)
                element.click()
            except Exception:
                print("手动:" + str(pageNo + 1))
                print("")
            WebDriverWait(self.driver, 10).until(
                EC.presence_of_element_located((By.XPATH, '//div[@class="question-container anwserView"]')))
            # self.driver.refresh()
            time.sleep(1)
            root = etree.HTML(self.driver.page_source)

            for_count = 1
            if pageNo + 1 == count:
                for_count = 2

            for r in range(0, for_count):
                main_element = root.xpath('//div[@class="question-container anwserView"]')[r]
                q_element = main_element.xpath('.//div[@class="question"]')[0]
                o_element = main_element.xpath('.//div[@class="options"]')[0]
                a_element = main_element.xpath('.//div[@class="answer-header"]')[0]
                c_element = main_element.xpath('.//div[@class="comment-content"]')[0]

                dict_data = dict()
                title = re.findall(r"class=\"content\">(.*?)</div>?", self.driver.page_source)[r]

                dict_data['序号'] = q_element.xpath('.//div[@class="content"]/span[1]/text()')[0]
                dict_data['题型'] = "".join(q_element.xpath('.//div[@class="title"]//text()')).replace("\n", "").replace(" ", "")
                dict_data['题干'] = "".join(q_element.xpath('.//div[@class="content-big-title"]//text()'))
                dict_data['问题'] = " ".join(q_element.xpath('.//div[@class="content"]/span[2]//text()'))
                dict_data['问题html'] = re.sub('data.*?\"', '', title, flags=re.M | re.S | re.I)
                dict_data['正确答案'] = a_element.xpath('.//text()')[1]
                dict_data['答案解析'] = "".join(c_element.xpath('.//text()'))

                no = 1
                for o in o_element.xpath('.//div[@class="option-text"]//text()'):
                    dict_data['选项_' + str(no)] = o
                    no += 1

                if pageNo == int(dict_data['序号'].replace(".", "")):
                    pageNo += 1
                    # 保存数据
                    HttpUtils.dict_to_csv(filename, dict_data)

            self.driver.back()
            time.sleep(1)
        print("==========================")
        self.driver.quit()




if __name__ == '__main__':
    chrome_get = Chrome_Get()
    chrome_get.get_contents("https://dxy.pub/q15URH")

